package com.baofu.paycore.service.converter;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.dal.model.*;
import com.baofu.paycore.service.facade.model.pay.FundDetailInfoRespDTO;
import com.baofu.paycore.service.facade.model.pay.PayQueryDetailRespDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeAccountRespDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeBankRespDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeDetailRespDTO;
import com.baofu.paycore.service.facade.model.transfer.TransferDetailRespDTO;
import com.baofu.paycore.service.facade.model.transfer.TransferFundDetailRespDTO;
import com.google.common.collect.Lists;
import com.system.commons.utils.BeanCopyUtil;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * DO对象转换DTO对象类
 *
 * User: xin deng  Date: 2017/1/12 ProjectName: paycore Version: 1.0
 */
public class DOToDTOConverter {

    public static PayQueryDetailRespDTO buildPayQueryDetailRespDTO(PayDO payDO, List<SubPayDO> subPayDOs){
        PayQueryDetailRespDTO payQueryDetailRespDTO = BeanCopyUtil.objConvert(payDO, PayQueryDetailRespDTO.class);
        if (CollectionUtils.isNotEmpty(subPayDOs)){
            List<FundDetailInfoRespDTO> list = Lists.newArrayList();
            subPayDOs.forEach(subPayDO ->
                list.add(BeanCopyUtil.objConvert(subPayDO, FundDetailInfoRespDTO.class))
            );
            payQueryDetailRespDTO.setFundDetailInfoRespDTOs(list);
        }
        return payQueryDetailRespDTO;
    }

    public static TransferDetailRespDTO buildTransferDetailRespDTO(TransferDO transferDO,
                                                                   List<SubTransferDO> subTransferDOs){
        TransferDetailRespDTO transferDetailRespDTO = BeanCopyUtil.objConvert(transferDO, TransferDetailRespDTO.class);
        if (CollectionUtils.isNotEmpty(subTransferDOs)){
            List<TransferFundDetailRespDTO> list = Lists.newArrayList();
            subTransferDOs.forEach(subPayDO ->
                    list.add(BeanCopyUtil.objConvert(subPayDO, TransferFundDetailRespDTO.class))
            );
            transferDetailRespDTO.setTransferFundDetailRespDTOs(list);
        }
        return transferDetailRespDTO;
    }

    public static RechargeDetailRespDTO buildRechargeDetailRespDTO(ClearDO clearDO,
                                                                   List<ClearAccountDO> clearAccountDOs,
                                                                   ClearBankDO clearBankDO,FreezeDO freezeDO){
        RechargeDetailRespDTO rechargeDetailRespDTO = BeanCopyUtil.objConvert(clearDO, RechargeDetailRespDTO.class);
        RechargeBankRespDTO rechargeBankRespDTO = BeanCopyUtil.objConvert(clearBankDO, RechargeBankRespDTO.class);
        rechargeDetailRespDTO.setRechargeBankRespDTO(rechargeBankRespDTO);
        if (CollectionUtils.isNotEmpty(clearAccountDOs)){
            List<RechargeAccountRespDTO> rechargeAccountRespDTOs = Lists.newArrayList();
            clearAccountDOs.forEach(clearAccountDO ->
                    rechargeAccountRespDTOs.add(BeanCopyUtil.objConvert(clearAccountDO, RechargeAccountRespDTO.class)));
            rechargeDetailRespDTO.setRechargeAccountRespDTOs(rechargeAccountRespDTOs);
        }
        if(freezeDO!=null){
            RechargeAccountRespDTO rechargeAccountRespDTO=new RechargeAccountRespDTO();
            freezeTOAccoutDTO(rechargeAccountRespDTO,freezeDO);
            rechargeAccountRespDTO.setFundType(FundTypeEnum.WITHDRAW_FREEZE.getCode());
            rechargeDetailRespDTO.getRechargeAccountRespDTOs().add(rechargeAccountRespDTO);
        }
        return rechargeDetailRespDTO;
    }

    private static void freezeTOAccoutDTO(RechargeAccountRespDTO rechargeAccountRespDTO, FreezeDO freezeDO) {
        BeanUtils.copyProperties(freezeDO,rechargeAccountRespDTO);
        rechargeAccountRespDTO.setFundDetailNo(freezeDO.getFreezeNo());
    }
}
